package exam.note.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import exam.aopLogger.Log;
import exam.common.Result;
import exam.note.domain.Note;
import exam.note.domain.NoteContent;
import exam.note.domain.NoteType;
import exam.note.mapper.NoteContentMapper;
import exam.note.mapper.NoteMapper;
import exam.note.mapper.NoteTypeMapper;
import exam.note.service.NoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.Node;

import java.lang.invoke.LambdaMetafactory;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/4/4 17:23
 * @since JDK 1.8
 */
@RestController
@Api(tags = {"笔记模块"})
@RequestMapping("note")
public class NoteController {

    @Autowired
    private NoteMapper noteMapper;

    @Autowired
    private NoteContentMapper noteContentMapper;

    @Autowired
    private NoteTypeMapper noteTypeMapper;

    @Autowired
    private NoteService noteService;

    @GetMapping("notetest")
    public JSONObject testController(){
        List<Note> noteList=noteService.list();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("data",noteList);
        return jsonObject;
    }

    @GetMapping("java")
    @ApiOperation("获取java文章列表")
    public JSONObject getJavaListController(@ApiParam("页码")@RequestParam(value = "page",defaultValue = "1")Integer page,
                                            @ApiParam("条数")@RequestParam(value = "size",defaultValue = "10")Integer size,
                                            @ApiParam("标题")@RequestParam(value = "title",required = false)String title,
                                            @ApiParam("备注")@RequestParam(value = "note",required = false)String note,
                                            @ApiParam("创建时间")@RequestParam(value = "createTime",required = false)String createTime){
        LambdaQueryWrapper<Note> noteLamada=new LambdaQueryWrapper<>();;
        noteLamada.eq(Note::getTypeId,4);
        if(!StringUtils.isEmpty(title)){
            noteLamada.like(Note::getTitle,title);
        }
        if(!StringUtils.isEmpty(note)){
            noteLamada.like(Note::getNote,note);
        }
        if(!StringUtils.isEmpty(createTime)){
            noteLamada.like(Note::getCreatetTime,createTime);
        }
        noteLamada.orderByDesc(Note::getCreatetTime);
        IPage<Note> noteIPage=noteService.page(new Page(page,size),noteLamada);
        noteTitleServer(noteIPage.getRecords());
        return Result.successObject(noteIPage);
    }


    public void noteTitleServer(List<Note> noteList){
        List<NoteType> noteTypeList=noteTypeMapper.selectList(null);
        noteList.forEach(e->{
            noteTypeList.forEach(t->{
                if(t.getId().equals(e.getTypeId())){
                    e.setType(t.getTypeName());
                }
            });
        });
    }


    @PutMapping()
    @Transactional
    @ApiOperation("笔记添加")
    public JSONObject addNoteController(@ApiParam("标题")@RequestParam("title")String title,
                                        @ApiParam("类型id")@RequestParam(value = "typeid",required =false)Integer typeId,
                                        @ApiParam("备注")@RequestParam(value = "note",required = false)String note,
                                        @ApiParam("笔记内容")@RequestParam("content")String content){
        JSONObject jsonObject=new JSONObject();

        if(StringUtils.isEmpty(title)||StringUtils.isEmpty(content)){
            jsonObject.put("code",1);
            jsonObject.put("msg","标题或内容不得为空");
            return jsonObject;
        }
        Note note1=new Note();
        note1.setVersion("1.1");
        note1.setTitle(title);
        note1.setNote(note);
        note1.setStatus(0);
        note1.setTypeId(typeId);
        note1.setCreatetTime(new Date());
        noteMapper.insert(note1);

        NoteContent noteContent=new NoteContent();
        noteContent.setContent(content);
        noteContent.setCreateTime(new Date());
        noteContent.setNoteId(note1.getId());
        noteContent.setTitle(note1.getTitle());
        noteContentMapper.insert(noteContent);

        jsonObject.put("code",0);
        jsonObject.put("msg","success");
        return jsonObject;
    }


    @PostMapping("noteList")
    @ApiOperation("笔记列表")
   // @Log("笔记列表")
    public JSONObject notePageController(@ApiParam("页码")@RequestParam(value = "pageNum",defaultValue = "1")Integer page,
                                         @ApiParam("条数")@RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                         @ApiParam("标题")@RequestParam(value = "title",required = false)String title,
                                         @ApiParam("备注")@RequestParam(value = "note",required = false)String note,
                                         @ApiParam("类型")@RequestParam(value = "typeId",required = false)Integer typeId){
        JSONObject jsonObject=new JSONObject();

        LambdaQueryWrapper<Note> noteLambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(title)){
            noteLambdaQueryWrapper.like(Note::getTitle,title);
        }
        if(!StringUtils.isEmpty(note)){
            noteLambdaQueryWrapper.like(Note::getNote,note);
        }
        if(!StringUtils.isEmpty(typeId)){
            noteLambdaQueryWrapper.eq(Note::getTypeId,typeId);
        }
        noteLambdaQueryWrapper.eq(Note::getStatus,0);
        noteLambdaQueryWrapper.orderByDesc(Note::getCreatetTime);
        IPage<Note> noteIPage=noteMapper.selectPage(new Page(page,pageSize),noteLambdaQueryWrapper);

        List<NoteType> noteTypes=noteTypeMapper.selectList(new LambdaQueryWrapper<>());

        List<Note> noteList=typeNoteDeal(noteIPage.getRecords(),noteTypes);

        jsonObject.put("code",0);
        jsonObject.put("msg","success");
        jsonObject.put("rows",noteList);
        jsonObject.put("total",noteIPage.getTotal());
        return jsonObject;

    }

    private List<Note> typeNoteDeal(List<Note> noteList,List<NoteType> noteTypes){
        noteList.stream().forEach(e->{
           NoteType noteType=noteTypes.stream().filter(d->d.getId().equals(e.getTypeId())).findFirst().orElse(null);
           e.setType(noteType==null?"无":noteType.getTypeName());
        });
        return noteList;
    }


    @PostMapping("/delete")
    @ApiOperation("笔记删除")
    @Log("笔记删除")
    public JSONObject deleteNoteController(@ApiParam("id数组")@RequestBody Integer[] ids){
        JSONObject jsonObject=new JSONObject();

        LambdaUpdateWrapper<Note> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(Note::getId,ids).set(Note::getStatus,1);
        noteMapper.update(null,lambdaUpdateWrapper);
        jsonObject.put("code",0);
        return jsonObject;
    }

    @GetMapping("{id}")
    @ApiModelProperty("获取具体的内容")
    @Log("笔记获取具体的内容")
    public JSONObject selectContentByIdController(@PathVariable("id")Integer id){
        JSONObject jsonObject=new JSONObject();

        if(StringUtils.isEmpty(id)){
            jsonObject.put("code",1);
            jsonObject.put("msg","id必须要值");
            return jsonObject;
        }

        LambdaQueryWrapper<Note> noteLambdaQueryWrapper=new LambdaQueryWrapper<>();
        noteLambdaQueryWrapper.eq(Note::getId,id);
        noteLambdaQueryWrapper.eq(Note::getStatus,0);

        Note note=noteMapper.selectOne(noteLambdaQueryWrapper);
        if(note==null){
            jsonObject.put("code",1);
            jsonObject.put("msg","对象不存在");
            return jsonObject;
        }

        NoteContent noteContent=noteContentMapper.selectById(note.getId());
        jsonObject.put("id",note.getId());
        jsonObject.put("title",note.getTitle());
        jsonObject.put("content",noteContent.getContent());
        jsonObject.put("createtime",noteContent.getCreateTime());

        return jsonObject;

    }


}
